package com.teamsun.controller;

import com.alibaba.fastjson.JSONObject;
import com.teamsun.annotation.PrivateHandlerMethod;
import com.teamsun.entity.VersionInfo;
import com.teamsun.system.ISysService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统相关操作
 */
@Controller
@RequestMapping("/rptTools/sys")
public class SysControler {

    @Autowired
    private ISysService sysService;

    /**
     * 版本更新页面跳转
     * @return
     */
    @RequestMapping("/version")
    @PrivateHandlerMethod
    public String version(HttpServletRequest request) {
        String versionId = request.getParameter("versionId");
        Map<String, Object> params = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(versionId)) {
            params.put("versionId", versionId);
        }
        // 默认只展示最新20条版本信息
        List<VersionInfo> versionInfos = sysService.queryVersionInfo(params, 1, 20);
        request.setAttribute("versionInfos", versionInfos);
        return "../WEB-INF/page/version";
    }

    /**
     * 版本更新页面跳转
     * @return
     */
    @RequestMapping("/versionMgr")
    @PrivateHandlerMethod
    public String versionMgr(HttpServletRequest request) {
        return "../WEB-INF/page/versionCfg";
    }

    /**
     * 查询版本信息
     */
    @RequestMapping("/queryVersionInfo")
    @ResponseBody
    @PrivateHandlerMethod
    public JSONObject queryVersionInfo(HttpServletRequest request, int page, int rows) {
        JSONObject jsonObject = new JSONObject();

        int startIndex = (page - 1) * rows + 1;
        int endIndex = page * rows;

        Map<String, Object> param = new HashMap<String, Object>();
        String versionId = request.getParameter("versionId");
        if(StringUtils.isNotBlank(versionId)) {
            versionId = versionId.trim();
            param.put("versionId", versionId);
        }

        // 查询版本信息
        Map<String, Object> userInfo = sysService.queryVersionInfoByPage(param, startIndex, endIndex);

        jsonObject.put("rows", userInfo.get("rows"));
        jsonObject.put("total", userInfo.get("total"));
        return jsonObject;
    }

    /**
     * 查询版本明细
     * @return
     */
    @RequestMapping("/versionDetail")
    @ResponseBody
    @PrivateHandlerMethod
    public JSONObject versionDetail(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String versionId = request.getParameter("versionId");
        Map<String, Object> params = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(versionId)) {
            params.put("versionId", versionId);
        }

        try {
            List<VersionInfo> versionInfos = sysService.queryVersionInfo(params, 1, 1);
            if (!CollectionUtils.isEmpty(versionInfos)) {
                jsonObject.put("status", "1");
                jsonObject.put("version", versionInfos.get(0));
            }
        } catch (Exception e) {
            jsonObject.put("status", "0");
            jsonObject.put("errorMsg", "查询版本信息失败");
            e.printStackTrace();
        }
        return jsonObject;
    }

    /**
     * 删除版本信息
     * @param request
     * @return
     */
    @RequestMapping("/deleteVersion")
    @ResponseBody
    @PrivateHandlerMethod
    public JSONObject deleteVersion(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();

        try {
            String versionId = request.getParameter("versionId");  // 版本ID

            // 删除版本信息
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("versionId", versionId);
            sysService.deleteVersionInfo(param);

            jsonObject.put("status", "1");
        } catch (Exception e) {
            jsonObject.put("status", "0");
            jsonObject.put("errorMsg", "删除版本信息失败");
            e.printStackTrace();
        }

        return jsonObject;
    }

    /**
     * 新增版本信息
     */
    @RequestMapping("/addVersionInfo")
    @ResponseBody
    @PrivateHandlerMethod
    public JSONObject addVersionInfo(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String errroMsg = "";
        try {
            String versionId = request.getParameter("versionId");  // 版本ID
            versionId = versionId.trim();
            String versionDesc = request.getParameter("versionDesc");  // 版本描述
            versionDesc = versionDesc.trim();

            // 查看版本ID是否重复
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("versionId", versionId);
            List<VersionInfo> versionInfos = sysService.queryVersionInfo(param, 1, 1);
            if(!CollectionUtils.isEmpty(versionInfos)) {
                jsonObject.put("status", "0");
                jsonObject.put("errorMsg", "版本ID[" + versionId + "]已经存在");
            } else {
                // 保存版本信息
                VersionInfo versionInfo = new VersionInfo();
                versionInfo.setVersionId(versionId);
                versionInfo.setVersionDesc(versionDesc);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                versionInfo.setCreateDate(format.format(new Date()));
                sysService.addVersionInfo(versionInfo);

                jsonObject.put("status", "1");
            }
        } catch (Exception e) {
            errroMsg = "新增版本信息失败";
            jsonObject.put("status", "0");
            jsonObject.put("errorMsg", errroMsg);
            e.printStackTrace();
        }
        return jsonObject;
    }

    /**
     * 修改版本信息
     * @param request
     * @return
     */
    @RequestMapping("/updateVersionInfo")
    @ResponseBody
    @PrivateHandlerMethod
    public JSONObject updateVersionInfo(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String errroMsg = "";
        try {
            String versionId = request.getParameter("versionId");  // 版本ID
            String versionDesc = request.getParameter("versionDesc");  // 版本名称
            versionId = versionId.trim();

            VersionInfo versionInfo = new VersionInfo();
            versionInfo.setVersionId(versionId);
            versionInfo.setVersionDesc(versionDesc);
            sysService.updateVersionInfo(versionInfo);

            jsonObject.put("status", "1");
        } catch (Exception e) {
            errroMsg = "修改版本信息失败";
            jsonObject.put("status", "0");
            jsonObject.put("errorMsg", errroMsg);
            e.printStackTrace();
        }
        return jsonObject;
    }
}
